package cqgans.app.sunao.com.cqgans.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.UUID;

import cqgans.app.sunao.com.cqgans.bean.ChangePWD;
import cqgans.app.sunao.com.cqgans.common.AppConstant;
import cqgans.app.sunao.com.cqgansnew.R;

/**
 * Created by sunao on 2017/6/30.
 */
@ContentView(R.layout.activity_changepwd)
public class ChangePwdActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.change_old_pwd)
    EditText change_old_pwd;
    @ViewInject(R.id.change_new_pwd)
    EditText change_new_pwd;

    @ViewInject(R.id.change_pwd_button)
    Button change_pwd_button;
    @ViewInject(R.id.reg_loading)
    RelativeLayout reg_loading;

    private String change_old_pwd_txt;
    private String change_new_pwd_txt;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        sharedPreferences = getSharedPreferences("cqgas", Context.MODE_PRIVATE);

        initLayout();
    }

    private void initLayout() {
        change_pwd_button.setOnClickListener(this);

        this.findViewById(R.id.back).setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitleBar("修改密码", 6);

    }

    private boolean verify() {
        UUID uuid = UUID.randomUUID();
        change_old_pwd_txt = String.valueOf(change_old_pwd.getText());
        change_new_pwd_txt = String.valueOf(change_new_pwd.getText());


        if (TextUtils.isEmpty(change_old_pwd_txt)) {
            Toast.makeText(this, "请输入原密码", Toast.LENGTH_SHORT).show();

            return false;
        }

        if (TextUtils.isEmpty(change_new_pwd_txt)) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();


            return false;
        }

        if (change_new_pwd_txt.length()<6||change_new_pwd_txt.length()>15) {
            Toast.makeText(this, "请输入6到15位密码", Toast.LENGTH_SHORT).show();


            return false;
        }

        return true;
    }

    private void changePWD() {
        ChangePWD change = new ChangePWD();
        change.setMobile(AppConstant.user.getMobile());
        change.setOldPass(change_old_pwd_txt);
        change.setNewPass(change_new_pwd_txt);
        RequestParams params = new RequestParams(AppConstant.URL + AppConstant.CHANGE_PASS_URL);


        Gson gson = new Gson();

        params.setAsJsonContent(true);
        params.setBodyContent(gson.toJson(change));
        params.addHeader("Content-Type", "application/json");

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.i(result);
                reg_loading.setVisibility(View.GONE);
                try {
                    JSONObject resultJson = new JSONObject(result);
                    if (resultJson.has("flag") && resultJson.getBoolean("flag")) {

//                        if (resultJson.has("data")){
//                            JSONObject data = resultJson.getJSONObject("data");
//                            JSONObject entity = data.getJSONObject("entity");
//                            User user = gson.fromJson(entity.toString(),User.class);
//                            AppConstant.user = user;
//                            if (user!=null&&user.getId()!=0){
//
//                                editor = sharedPreferences.edit();//获取编辑器
//                                editor.putString("mobile", user.getMobile());
//                                editor.putInt("id", user.getId());
//                                editor.putString("userNumber",user.getUserNumber());
//                                editor.putString("userName",user.getUsername());
//                                editor.putString("password",user.getPassword());
//                                editor.commit();
//
//                                RegisterActivity.this.startActivity(new Intent(RegisterActivity.this,HomeActivity.class));
//                                finish();
//                            }
//                        }
                        Toast.makeText(ChangePwdActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                        finish();

                    } else {
                        Toast.makeText(ChangePwdActivity.this, "修改失败，请重试", Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                reg_loading.setVisibility(View.GONE);
                LogUtil.e(ex.getMessage());
                Toast.makeText(ChangePwdActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                reg_loading.setVisibility(View.GONE);
            }

            @Override
            public void onFinished() {

            }
        });


    }


    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.change_pwd_button:
                if (verify()) {
                    changePWD();
                } else {
//                    Toast.makeText(this, "请输入必填字段并检查输入内容是否正确", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.back:
                finish();
                break;
        }
    }



}
